﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MonsterCreater : MonoBehaviour
{
    public CreatBatch[] creatBatch;
    private void OnGUI()
    {
        if (GUILayout.Button("开启协同程序"))
        {
            StartCoroutine("Fun"); //开启后可以用StopCoroutine("Fun")停止,建议使用这种方式

            //StartCoroutine(Fun());  这种方式开启之后不能指定停止这个协同,只能用StopAllCoroutines()停止
        }
    }

    //定义协同方法
    private IEnumerator Fun()
    {
        for (int i = 0; i < creatBatch.Length; i++)
        {
            var batch = creatBatch[i];
            for (int n = 0; n < batch.n; n++)
            {
                print("生成:" + batch.monsterName);
                yield return new WaitForSeconds(batch.interval);
            }
            yield return new WaitForSeconds(batch.batchInterval);
        }
    }
}

[System.Serializable]
public class CreatBatch
{
    public int n;
    public float interval;
    public string monsterName;
    public float batchInterval;

    public CreatBatch(int n, float interval, string monsterName, float batchInterval)
    {
        this.n = n;//几个
        this.monsterName = monsterName;//什么怪
        this.interval = interval;//没一个的间隔
        this.batchInterval = batchInterval;//下一波的等待时间
    }
}